﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Media.Imaging;
using YoucaiReader.Helper;
using System.Windows.Input;
using System.Windows.Threading;
using Assistant;
using YoucaiReader.Pages;
using YoucaiReader.Entities;

namespace YoucaiReader.UserControls
{
    public partial class ProgressControl : UserControl
    {
        private bool progressMinus_ButtonDown;
        private bool progressPlus_ButtonDown;
        private DispatcherTimer UpdateTimeMinus = new DispatcherTimer();
        private DispatcherTimer UpdateTimePlus = new DispatcherTimer();

        public ProgressControl()
        {
            this.InitializeComponent();
        }

        private BookReaderPage bookReaderPage;
        public ProgressControl(BookReaderPage bookReaderPage)
            : this()
        {
            this.bookReaderPage = bookReaderPage;
            if (SettingHelper.IsSettings(string.Concat(App.ReaderFileName, "CurrentReaderBook")))
            {
                LocalBookProgressListModel valueFromSettings = SettingHelper.GetValueFromSettings(string.Concat(App.ReaderFileName, "CurrentReaderBook")) as LocalBookProgressListModel;
                this.progressSlider.Value = valueFromSettings.double_ReaderProgress_M * 100;
                this.progressText.Text = GetNowTime.SetPercentageText(this.progressSlider.Value / 100);
                this.UpdateButton_Value();
            }
        }


        private void progressMinus_ButtonUp()
        {
            if (this.progressMinus_ButtonDown)
            {
                this.PopupProgressPanel((float)Math.Round(this.progressSlider.Value, 2));
                this.UpdateTimeMinus.Stop();
                this.UpdateTimeMinus.Tick -= new EventHandler(this.UpdateTimeMinus_Tick);
                this.progressMinus_ButtonDown = false;
                this.UpdateButton_Value();
            }
        }

        private void progressMinus_MouseLeave(object sender, MouseEventArgs e)
        {
            this.progressMinus_ButtonUp();
        }

        private void progressMinus_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.progressMinus_SliderValue();
            this.progressMinus_ButtonDown = true;
            this.UpdateTimeMinus.Tick += new EventHandler(this.UpdateTimeMinus_Tick);
            this.UpdateTimeMinus.Start();
            this.UpdateTimeMinus.Interval = TimeSpan.FromMilliseconds(100);
        }

        private void progressMinus_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            this.progressMinus_ButtonUp();
        }

        private void progressMinus_SliderValue()
        {
            if (this.progressSlider.Value - 1 < 0)
            {
                this.progressSlider.Value = 0;
                return;
            }
            Slider value = this.progressSlider;
            value.Value = value.Value - 1;
        }

        private void progressPlus_ButtonUp()
        {
            if (this.progressPlus_ButtonDown)
            {
                //this.PopupProgressPanel((float)Math.Round(this.progressSlider.Value, 2));
                this.bookReaderPage.BookReader_ProgressMenu((float)Math.Round(this.progressSlider.Value, 2));
                this.UpdateTimePlus.Stop();
                this.UpdateTimePlus.Tick -= new EventHandler(this.UpdateTimePlus_Tick);
                this.progressPlus_ButtonDown = false;
                this.UpdateButton_Value();
            }
        }

        private void progressPlus_MouseLeave(object sender, MouseEventArgs e)
        {
            this.progressPlus_ButtonUp();
        }

        private void progressPlus_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.progressPlus_SliderValue();
            this.progressPlus_ButtonDown = true;
            this.UpdateTimePlus.Tick += new EventHandler(this.UpdateTimePlus_Tick);
            this.UpdateTimePlus.Start();
            this.UpdateTimePlus.Interval = TimeSpan.FromMilliseconds(100);
        }

        private void progressPlus_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            this.progressPlus_ButtonUp();
        }

        private void progressPlus_SliderValue()
        {
            if (this.progressSlider.Value + 1 > 100)
            {
                this.progressSlider.Value = 100;
                return;
            }
            Slider value = this.progressSlider;
            value.Value = value.Value + 1;
        }

        private void progressSlider_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
        {
            //this.PopupProgressPanel((float)Math.Round(this.progressSlider.Value, 2));
            this.bookReaderPage.BookReader_ProgressMenu((float)Math.Round(this.progressSlider.Value, 2));
            this.UpdateButton_Value();
        }

        private void progressSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            this.progressText.Text = GetNowTime.SetPercentageText(this.progressSlider.Value / 100);
        }

        private void UpdateButton_Value()
        {
            if (this.progressSlider.Value == 0)
            {
                this.progressPlus.Source = new BitmapImage(new Uri("/Images/ReadIcon/arrow_r_nor.png", UriKind.Relative));
                this.progressMinus.Source = new BitmapImage(new Uri("/Images/ReadIcon/arrow_l_nor2.png", UriKind.Relative));
                return;
            }
            if (this.progressSlider.Value == 100)
            {
                this.progressPlus.Source = new BitmapImage(new Uri("/Images/ReadIcon/arrow_r_nor2.png", UriKind.Relative));
                this.progressMinus.Source = new BitmapImage(new Uri("/Images/ReadIcon/arrow_l_nor.png", UriKind.Relative));
                return;
            }
            this.progressPlus.Source = new BitmapImage(new Uri("/Images/ReadIcon/arrow_r_nor.png", UriKind.Relative));
            this.progressMinus.Source = new BitmapImage(new Uri("/Images/ReadIcon/arrow_l_nor.png", UriKind.Relative));
        }

        private void UpdateTimeMinus_Tick(object sender, EventArgs e)
        {
            this.progressMinus_SliderValue();
        }


        private void UpdateTimePlus_Tick(object sender, EventArgs e)
        {
            this.progressPlus_SliderValue();
        }

    }
}
